﻿using System;
using System.Collections.Generic;
using System.Text;
using SourceCode.K2ROM;

namespace Lisphi.Lab.K2Lib
{
    public class K2Library
    {
        private string _server;
        private string _connStr;

        private Connection OpenConnection()
        {
            Connection conn = new Connection();
            conn.Open(this._server, this._connStr);
            return conn;
        }

        private void AssignDataFields(DataFields dataFields, Dictionary<string, string> parameters)
        {
            if (parameters == null) return;
            foreach (KeyValuePair<string, string> pair in parameters)
            {
                dataFields[pair.Key].Value = pair.Value;
            }
        }

        public string ProcessName { get; set; }

        public string Platform { get { return "ASP"; } }

        public K2Library(string server, string connStr)
        {
            this._server = server;
            this._connStr = connStr;
        }

        public ProcessInstance StartProcess(string folio, Dictionary<string, string> parameters)
        {
            Connection conn = this.OpenConnection();
            try
            {
                ProcessInstance procInst = conn.CreateProcessInstance(this.ProcessName);
                procInst.Folio = folio;
                this.AssignDataFields(procInst.DataFields, parameters);
                conn.StartProcessInstance(procInst, true);
                return procInst;
            }
            finally
            {
                conn.Close();
            }
        }

        public Worklist OpenWorklist(int procInstID)
        {
            Connection conn = this.OpenConnection();
            try
            {
                WorklistCriteria criteria = new WorklistCriteria();
                criteria.Platform = this.Platform;
                criteria.AddFilterField(WCField.ProcessID, WCCompare.Equal, procInstID);
                return conn.OpenWorklist(criteria);
            }
            finally
            {
                conn.Close();
            }
        }

        public WorklistItem OpenWorklistItem(string serialNumber)
        {
            Connection conn = this.OpenConnection();
            try
            {
                return conn.OpenWorklistItem(serialNumber, this.Platform);
            }
            finally
            {
                conn.Close();
            }
        }

        public void MoveWorklistItem(string serialNumber, Dictionary<string, string> parameters)
        {
            Connection conn = this.OpenConnection();
            try
            {
                WorklistItem worklistItem = conn.OpenWorklistItem(serialNumber, this.Platform);
                if (worklistItem == null) return;
                ProcessInstance procInst = worklistItem.ProcessInstance;
                this.AssignDataFields(procInst.DataFields, parameters);
                worklistItem.Update();
                worklistItem.Finish();
            }
            finally
            {
                conn.Close();
            }
        }

        public void GotoActivity(string serialNumber, string activity, Dictionary<string, string> parameters)
        {
            Connection conn = this.OpenConnection();
            try
            {
                WorklistItem worklistItem = conn.OpenWorklistItem(serialNumber, this.Platform);
                if (worklistItem == null) return;
                ProcessInstance procInst = worklistItem.ProcessInstance;
                this.AssignDataFields(procInst.DataFields, parameters);
                worklistItem.Update();
                worklistItem.GotoActivity(activity);
                worklistItem.Finish();
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
